package com.zzy.action;



import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zzy.model.User;
import com.zzy.service.UserService;

@Controller
public class LoginAction {

	@Autowired
	UserService userService;
	
	
	@RequestMapping("/login")
	 public String login() {
	    
	      return "login";
	}
	
	@RequestMapping("/index")
	 public String index(HttpServletRequest request,Model model) {
		String base=request.getContextPath();
		model.addAttribute("base", base);
	      return "index";
	}
	
	
	

	@RequestMapping("/loginAction")
	public String admins(HttpServletRequest request, Model model) throws UnsupportedEncodingException {
		
		String username=request.getParameter("username");
		username=new  String(username.getBytes("ISO-8859-1"),"utf-8");
		String password=request.getParameter("password");
		
		
		User user=userService.login(username, password);
		if (null==user) {
			model.addAttribute("error", "用户名或密码错误！");
			return "login";
		} else {
			 HttpSession session = request.getSession();
			 session.setAttribute("loginUser", user);
			return "redirect:/index";
		}
	}

}
